<section class="uk-section uk-section-xsmall" data-uk-height-viewport="expand: true">
    <div class="uk-container">
        <div class="uk-grid uk-grid-small uk-margin-small" data-uk-grid>
            <div class="uk-width-expand uk-heading-line">
                <h3 class="uk-h3"><i class="fas fa-puzzle-piece"></i> <?= $this->lang->line('admin_nav_manage_modules'); ?></h3>
            </div>
            <div class="uk-width-auto">
                <a href="" class="uk-icon-button"><i class="fas fa-info"></i></a>
            </div>
        </div>
        <div class="uk-card uk-card-default uk-card-body">
            <div class="uk-overflow-auto">
                <table class="uk-table uk-table-middle uk-table-divider uk-table-small">
                    <thead>
                        <tr>
                            <th class="uk-table-expand"><?= $this->lang->line('table_header_module'); ?></th>
                            <th class="uk-width-small uk-text-center"><?= $this->lang->line('table_header_actions'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($this->admin_model->getModules() as $module) : ?>
                            <tr>
                                <td><?= $module->name; ?></td>
                                <td class="uk-text-center" uk-margin>
                                    <?php if ($module->status == 1) : ?>
                                        <button class="uk-button uk-button-danger" value="<?= $module->id ?>" id="button_disable<?= $module->id ?>" onclick="DisableModule(event, this.value)"><i class="fas fa-eye-slash"></i></button>
                                    <?php else : ?>
                                        <button class="uk-button uk-button-primary" value="<?= $module->id ?>" id="button_enable<?= $module->id ?>" onclick="EnableModule(event, this.value)"><i class="fas fa-eye"></i></button>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>

<script>
    var csrfName = "<?= $this->security->get_csrf_token_name() ?>";
    var csrfHash = "<?= $this->security->get_csrf_hash() ?>";

    function EnableModule(e, value) {
        e.preventDefault();

        $.ajax({
            url: "<?= base_url($lang . '/admin/modules/enable'); ?>",
            method: "POST",
            data: {
                value,
                [globalThis.csrfName]: globalThis.csrfHash
            },
            dataType: "text",
            beforeSend: function() {
                $.amaran({
                    'theme': 'awesome info',
                    'content': {
                        title: '<?= $this->lang->line('notification_title_info'); ?>',
                        message: '<?= $this->lang->line('notification_checking'); ?>',
                        info: '',
                        icon: 'fas fa-sign-in-alt'
                    },
                    'delay': 1000,
                    'position': 'top right',
                    'inEffect': 'slideRight',
                    'outEffect': 'slideRight'
                });
            },
            success: function(response) {
                if (!response)
                    alert(response);

                if (response) {
                    $.amaran({
                        'theme': 'awesome ok',
                        'content': {
                            title: '<?= $this->lang->line('notification_title_success'); ?>',
                            message: '<?= $this->lang->line('notification_module_enabled'); ?>',
                            info: '',
                            icon: 'fas fa-check-circle'
                        },
                        'delay': 5000,
                        'position': 'top right',
                        'inEffect': 'slideRight',
                        'outEffect': 'slideRight'
                    });
                }
                setTimeout(function() {
                    location.reload();
                }, 500);
            }
        });
    }

    function DisableModule(e, value) {
        e.preventDefault();

        $.ajax({
            url: "<?= base_url($lang . '/admin/modules/disable'); ?>",
            method: "POST",
            data: {
                value,
                [globalThis.csrfName]: globalThis.csrfHash
            },
            dataType: "text",
            beforeSend: function() {
                $.amaran({
                    'theme': 'awesome info',
                    'content': {
                        title: '<?= $this->lang->line('notification_title_info'); ?>',
                        message: '<?= $this->lang->line('notification_checking'); ?>',
                        info: '',
                        icon: 'fas fa-sign-in-alt'
                    },
                    'delay': 1000,
                    'position': 'top right',
                    'inEffect': 'slideRight',
                    'outEffect': 'slideRight'
                });
            },
            success: function(response) {
                if (!response)
                    alert(response);

                if (response) {
                    $.amaran({
                        'theme': 'awesome ok',
                        'content': {
                            title: '<?= $this->lang->line('notification_title_success'); ?>',
                            message: '<?= $this->lang->line('notification_module_disabled'); ?>',
                            info: '',
                            icon: 'fas fa-check-circle'
                        },
                        'delay': 5000,
                        'position': 'top right',
                        'inEffect': 'slideRight',
                        'outEffect': 'slideRight'
                    });
                }
                setTimeout(function() {
                    location.reload();
                }, 500);
            }
        });
    }
</script>